package com.wan.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import com.google.gson.JsonObject
import com.wan.R
import com.wan.adapter.HomeRvAdapter
import com.wan.base.BaseBean
import com.wan.entity.HomeArtEntity
import com.wan.entity.HomeArtItemEntity
import com.wan.uitls.*
import kotlinx.android.synthetic.main.activity_collection.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


/**
 * 收藏
 */
class CollectionActivity : AppCompatActivity(), ItemClickListener, ItemRightViewListener {


    private val mThreadName = this.javaClass.name


    private val dataList = ArrayList<HomeArtItemEntity>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_collection)

        initView()
    }

    private fun initView() {
        val layoutManager = LinearLayoutManager(this)
        collection_data_rv.layoutManager = layoutManager

        val homeRvAdapter = HomeRvAdapter(this, dataList, R.layout.fragment_home_item_layout)
        collection_data_rv.adapter = homeRvAdapter
        homeRvAdapter.setItemClickListener(this)


        val manager = ApiManager(this)
        var loginEntity = CommonUtils.getLogginMessage(this)
        manager.getCollectionList(0, loginEntity!!.token)
                .enqueue(object : Callback<BaseBean<HomeArtEntity>> {
                    override fun onFailure(call: Call<BaseBean<HomeArtEntity>>, t: Throwable) {
                        Log.e(mThreadName, "onFailure : ${call.execute().body()}", t)
                    }

                    override fun onResponse(call: Call<BaseBean<HomeArtEntity>>, response: Response<BaseBean<HomeArtEntity>>) {
                        Log.d(mThreadName, "onResponse : 收藏==>{${response.body()}}")
                        response.body()!!.data.datas
                        dataList.addAll(response.body()!!.data.datas)
                        homeRvAdapter.notifyDataSetChanged()
                    }
                })
    }

    override fun onItemClickListener(position: Int) {
        val artItemEntity = dataList[position]
        CommonUtils.sendIntent(this, WebActivity::class.java, artItemEntity.link, artItemEntity.title)
    }

    override fun onRightViewClickListener(position: Int) {
        CommonUtils.toastWord(this, "right pic view click")
    }

    override fun onDestroy() {
        super.onDestroy()
        RetrofitHelper.get().cancelCall(this)
    }
}
